﻿using System;
using System.Collections;
using System.Reflection;
using log4net;
using log4net.Config;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.ServiceModel;
using System.ComponentModel;
using System.ServiceModel.Security;
using System.Collections.Generic;
using System.ServiceModel.Description;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace HaiFang
{
    [Serializable]
    public class ProxyCompanyInfo
    {
        private static string PROCEDURE_QueryCompanyInfo = "QueryCompanyInfo";
        private static string PROCEDURE_InsertCompanyInfo = "InsertCompanyInfo";

        //@CurrUserID			int = null,					--管理员编号
        //@AutoID				bigint=0					--查询所有时传0，否则传编号值
        public static IList<CompanyInfo> QueryCompanyInfo(int CurrUserID, int AutoID)
        {
            IList<CompanyInfo> list = new List<CompanyInfo>();
            try
            {
                using (SqlHelper helper = new SqlHelper())
                {
                    #region 构造参数
                    string comandText = PROCEDURE_QueryCompanyInfo;
                    List<SqlParameter> Parameters = new List<SqlParameter>();
                    Parameters.Add((new SqlParameter("@CurrUserID", CurrUserID)));
                    Parameters.Add(new SqlParameter("@AutoID", AutoID));
                    #endregion

                    #region 获取执行结果
                    DataTable dt = helper.ExecuteDataTable(comandText, CommandType.StoredProcedure, Parameters);
                    list = helper.GetObjects<CompanyInfo>(dt);
                    #endregion
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            return list;
        }

        //@CurrUserID			int = null,					--管理员编号
        //@UseFlag			tinyint,					--1:insert 2:update   3:delete
        //@AutoID				bigint=0,					--@UseFlag为1时传0，其它操作传编号值
        //@CompanyName		varchar(200),
        //@Description		varchar(128),
        //@ErrorMSG			int	output					--100：厂商存在 0：正确；其他：错误
        public static void InsertCompanyInfo(int CurrUserID,int UseFlag,int AutoID, string CompanyName,string Description,out int ErrorMSG)
        {
            ErrorMSG = -1;
            try
            {
                using (SqlHelper helper = new SqlHelper())
                {
                    #region 构造参数
                    string comandText = PROCEDURE_InsertCompanyInfo;
                    List<SqlParameter> Parameters = new List<SqlParameter>();
                    Parameters.Add((new SqlParameter("@CurrUserID", CurrUserID)));
                    Parameters.Add(new SqlParameter("@UseFlag", UseFlag));
                    Parameters.Add(new SqlParameter("@AutoID", AutoID));
                    Parameters.Add(new SqlParameter("@CompanyName", CompanyName));
                    Parameters.Add(new SqlParameter("@Description", Description));
                    Parameters.Add(new SqlParameter()
                    {
                        ParameterName = "@ErrorMSG",
                        SqlDbType = SqlDbType.Int,
                        Direction = ParameterDirection.Output,
                        Value = ErrorMSG
                    });
                    #endregion

                    #region 获取执行结果
                    helper.Execute(comandText, CommandType.StoredProcedure, Parameters);
                    #endregion

                    #region 获取output值
                    foreach (SqlParameter sp in Parameters)
                    {
                        if (sp.ParameterName == "@ErrorMSG")
                        {
                            ErrorMSG = helper.GetInt(sp.Value);
                            break;
                        }
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
        }
    }

    [Serializable]
    public class CompanyInfo
    {
        public int DisplayID { get; set; }
        public int AutoID { get; set; }
        public string GUID { get; set; }
        public string CompanyName { get; set; }
        public DateTime CreateTime { get; set; }
        public string Description { get; set; }
        public int DeleteFlag { get; set; }
    }
}
